﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml;
using System.Collections.ObjectModel;
using System.Collections;

namespace RssV_m_v.ViewModel
{
    public class RssService
    {
        MainViewModel mvm;
        string nomJournal;
        public RssService(MainViewModel mvm,string nomJournal)
        {
            this.mvm = mvm;
            this.nomJournal = nomJournal;
        }
        public void chargerRSS(string url)
        {
            if (String.IsNullOrEmpty(url))
                MessageBox.Show("adresse non valide");

            else
            {
                if (!url.Contains("http://"))
                    url = "http://" + url; WebClient client = new WebClient();
                client.OpenReadCompleted += new OpenReadCompletedEventHandler(chargerRSSCompleted);
                client.OpenReadAsync(new Uri(url));

            }
        }
        private void chargerRSSCompleted(object sender, OpenReadCompletedEventArgs e)
        {

            if (e.Error == null)
            {

                XmlReader flux = XmlReader.Create(e.Result);
                ObservableCollection<News> journalTraite = new ObservableCollection<News>();
                journalTraite = traitementDuJournal(flux);
                if(nomJournal.CompareTo("parisien")==0) mvm.ListItemP = journalTraite;
                if (nomJournal.CompareTo("figaro") == 0) mvm.ListItemF = journalTraite;
                if (nomJournal.CompareTo("leMonde") == 0) mvm.ListItemM = journalTraite;
                
                /*while (flux.Read())
                {
                    //switch (flux.NodeType)
                    //{      

                    if (flux.NodeType == XmlNodeType.Element)
                    {
                        string b = flux.Name;
                        if (b.CompareTo("title") == 0)
                        {
                            s = new SyndicationItemPerso();
                            //flux.MoveToFirstAttribute();
                            //   string v = flux.MoveToFirstAttribute().ToString();
                           // int t = flux.Depth;
                            flux.MoveToFirstAttribute();
                //            flux.MoveToNextAttribute();
                            //string eza = flux.ReadOuterXml();
                            string t = flux.ReadElementContentAsString();
                         //   s.Titre = flux.ReadOuterXml();
                            s.Titre = t;
                        //    mvm.update(s, nomJournal);
                        }
                        else if (b.CompareTo("enclosure") == 0)
                        {

                            flux.MoveToFirstAttribute();
                            s.Url = flux.Value;
                           // mvm.update(s,nomJournal);
                            tempcols.Add(s);
                            
                        }
                    }
                }
                mvm.ListItemP = tempcols;
                //mvm.ListItem = tempcols;

                /*if (flux.NodeType == XmlNodeType.Text)
                {
                    string c = flux.Value;
                }*/



                /*  case XmlNodeType.Element: // The node is an element.
                      string t = flux.Name;
                      break;
                  case XmlNodeType.Text: //Display the text in each element.
                      string a = flux.Value;
                      break;
                  case XmlNodeType.EndElement: //Display the end of the element.
                      string b = flux.Name;
                      break;
              }*/


                /*SyndicationFeed feeds = SyndicationFeed.Load(XmlReader.Create(e.Result));
                mvm.WelcomeTitle = feeds.Title.Text;
                mvm.Titre = feeds.Title.Text;
                ObservableCollection<string> temp = new ObservableCollection<string>();
                ObservableCollection<News> listNews = new ObservableCollection<News>();
                //SyndicationItem si = new SyndicationItemPerso();
                //temp2 = feeds.Items;
                foreach (SyndicationItem item in feeds.Items)
                {
                    SyndicationItemPerso s = new SyndicationItemPerso();
                    s.Title = item.Title;
                    s.image(e);

                    News n = new News();
                    n.Title = item.Title.Text;

                    mvm.update(n);
                    mvm.update(s);

                }
                // mvm.ListTitre = temp;
                // mvm.ListItem = temp2;
                //  mvm.Titre = "toto";

            }*/


            }

        }

        private ObservableCollection<News> traitementDuJournal(XmlReader flux)
        {
            News s = new News();
            ObservableCollection<News> tempcols = new ObservableCollection<News>();
            while (flux.Read())
            {
                 

                if (flux.NodeType == XmlNodeType.Element)
                {
                    string b = flux.Name;
                    if (nomJournal.CompareTo("parisien") == 0)
                    {
                        if (b.CompareTo("title") == 0)
                        {
                            s = new News();
                            flux.MoveToFirstAttribute();
                            string t = flux.ReadElementContentAsString();
                            s.Title = t;
                           
                        }
                        else if (b.CompareTo("description") == 0)
                        {
                            flux.MoveToFirstAttribute();
                            s.Description = flux.ReadElementContentAsString();
                        }
                        else if (b.CompareTo("enclosure") == 0)
                        {

                            flux.MoveToFirstAttribute();
                            s.UrlImage = flux.Value;
                            // mvm.update(s,nomJournal);
                            tempcols.Add(s);

                        }
                    }
                    else if (nomJournal.CompareTo("figaro") == 0)
                    {
                        if (b.CompareTo("title") == 0)
                        {
                            s = new News();
                            flux.MoveToFirstAttribute();
                            string t = flux.ReadElementContentAsString();
                            s.Title = t;
                            //    mvm.update(s, nomJournal);
                        }
                        else if (b.CompareTo("enclosure") == 0)
                        {

                            flux.MoveToFirstAttribute();
                            s.UrlImage = flux.Value;
                            // mvm.update(s,nomJournal);
                            tempcols.Add(s);

                        }
                    }
                    else if (nomJournal.CompareTo("leMonde") == 0)
                    {
                        if (b.CompareTo("title") == 0)
                        {
                            s = new News();
                            flux.MoveToFirstAttribute();
                            string t = flux.ReadElementContentAsString();
                            s.Title = t;
                            
                            //    mvm.update(s, nomJournal);
                        }
                        else if (b.CompareTo("enclosure") == 0)
                        {

                            flux.MoveToFirstAttribute();
                            s.UrlImage = flux.Value;
                            tempcols.Add(s);

                        }
                    }
                }
            }
            return tempcols;
        }
      
    }
}


   
